package com.dapangdong.testdemo.controller.test;


import cn.hutool.core.text.StrPool;
import com.atguigu.gulimall.basic.minio.config.MinioProperties;
import com.atguigu.gulimall.basic.minio.service.MinioService;
import com.dapangdong.testdemo.utils.FileTypeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  minio 文件处理（上传，下载，获取文件地址等）
 * </p>
 *
 * @author : yhd
 * @program: gulimall
 * @since : 2023-05-25 15:59
 */
@RequiredArgsConstructor
@RequestMapping("/minio")
@Slf4j
@RestController
@Api(tags = "minio文件处理模块")
public class MinioController {

    private final MinioService minioService;
    private final MinioProperties minioProperties;


    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public Object uploadFile(MultipartFile file, String bucketName) {
        String fileType = FileTypeUtils.getFileType(file);
        if (fileType != null) {
            return minioService.putObject(file, bucketName, fileType);
        }
        return "不支持的文件格式。请确认格式,重新上传！！！";
    }

    @ApiOperation(value = "创建bucket")
    @PostMapping("/addBucket/{bucketName}")
    public String addBucket(@PathVariable String bucketName) {
        minioService.makeBucket(bucketName);
        return "bucketName为"+bucketName+",创建成功！！！";
    }

    @ApiOperation(value = "列出存储桶中的所有对象名称")
    @GetMapping("/show/{bucketName}")
    public List<String> show(@PathVariable String bucketName) {
        return minioService.listObjectNames(bucketName);
    }

    @ApiOperation(value = "列出所有存储桶名称")
    @GetMapping("/showBucketName")
    public List<String> showBucketName() {
        return minioService.listBucketName();
    }

    @ApiOperation(value = "列出存储桶中的所有对象名称")
    @GetMapping("/showListObjectNameAndDownloadUrl/{bucketName}")
    public List<Map<String, String>> showListObjectNameAndDownloadUrl(@PathVariable String bucketName) {
        List<Map<String, String>> returnList = new ArrayList<>();
        //获取该桶下的所有文件
        List<String> listObjectNames = minioService.listObjectNames(bucketName);
        //文件路径
        String fileUrl = minioProperties.getEndpoint() + StrPool.COLON + minioProperties.getPort() + "/" + bucketName + "/" ;
        listObjectNames.forEach(System.out::println);
        for (String listObjectName : listObjectNames) {
            Map<String, String> map = new HashMap<>(2);
            map.put("fileName", listObjectName);
            map.put("fileUrl", fileUrl + listObjectName);
            returnList.add(map);
        }
        return returnList;
    }

    @ApiOperation(value = "根据桶名删除桶")
    @DeleteMapping("/removeBucket/{bucketName}")
    public String delBucketName(@PathVariable String bucketName) {
        return minioService.removeBucket(bucketName) ? "删除成功" : "删除失败";
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping("/removeObject/{bucketName}/{objectName}")
    public String delObject(@PathVariable("bucketName") String bucketName, @PathVariable("objectName") String objectName) {
        return minioService.removeObject(bucketName, objectName) ? "删除成功" : "删除失败";
    }

    @ApiOperation(value = "批量删除文件")
    @DeleteMapping("/removeListObject/{bucketName}")
    public String delListObject(@PathVariable("bucketName") String bucketName, @RequestBody List<String> objectNameList) {
        return minioService.removeListObject(bucketName, objectNameList) ? "删除成功" : "删除失败";
    }

//    @ApiOperation(value = "创建bucket")
//    @PostMapping("/download/{bucketName}/{objectName}")
//    public void download(HttpServletResponse response, @PathVariable("bucketName") String bucketName, @PathVariable("objectName") String objectName) {
//        InputStream in = null;
//        try {
//            in = minioService.downloadObject(bucketName, objectName);
//            response.setHeader("Content-Disposition", "attachment;filename="
//                    + URLEncoder.encode(objectName, "UTF-8"));
//            response.setCharacterEncoding("UTF-8");
//            //将字节从InputStream复制到OutputStream 。
//            IOUtils.copy(in, response.getOutputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (in != null) {
//                try {
//                    in.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//    }
//





}
